package cn.wit.summit.chwan.ui.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.event.BaseEvent;
import cn.wit.summit.chwan.ui.activity.DailyRecommendationActivity;
import cn.wit.summit.chwan.ui.activity.MainActivity;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.SystemBarTintManager;

/**
 * Created by tangbingliang on 17/2/16.
 */

public class MyFragmentActivity extends FragmentActivity {

    private LinearLayout rootContentLl;
    private ImageView baseLineIv;
    private ImageView barTitleIv;
    protected TextView barTitleTx;
    private RelativeLayout barTitleRl;
    private ImageView barBackIv;
    protected LinearLayout barBackLl;
    private TextView barRightTx;
    protected ImageView barRightIv;
    private RelativeLayout barRightRl;
    protected RelativeLayout rootBarRl;
    private LinearLayout baseTopLl;
    public LinearLayout topLayout;
    private ImageView barHeightIv;

    protected Context mContext;
    protected Activity mAty;

    protected boolean customHeader = false;

    @Subscribe
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!customHeader)
        super.setContentView(R.layout.activity_mybase);
        mAty = this;
        mContext = this;
        if (barHeightIv==null)
            barHeightIv = (ImageView) findViewById(R.id.barHeightIv);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LogUtils.e("getStateBarHeight():"+getStateBarHeight());
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(false);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

            if (mAty instanceof DailyRecommendationActivity) {
                barHeightIv.setVisibility(View.GONE);
            } else {
                barHeightIv.setVisibility(View.VISIBLE);
                tintManager.setStatusBarTintResource(R.drawable.header_gradient_bg);// 通知栏所需颜色
            }
        }
        EventBus.getDefault().register(mContext);
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void setContentView(int layoutResID) {
        if (customHeader){
            super.setContentView(layoutResID);
            return;
        }

        //标题栏最外层
        rootBarRl = (RelativeLayout) findViewById(R.id.root_bar_rl);
        //标题栏标题外层
        barTitleRl = (RelativeLayout) findViewById(R.id.bar_title_rl);

        //标题栏标题文字控件
        barTitleTx = (TextView) findViewById(R.id.bar_title_tx);
        //标题栏标题图片控件
        barTitleIv = (ImageView) findViewById(R.id.bar_title_iv);

        //标题栏返回外层
        barBackLl = (LinearLayout) findViewById(R.id.bar_back_ll);
        barBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //标题栏返回图片控件
        barBackIv = (ImageView) findViewById(R.id.bar_back_iv);
        //标题栏分割点
        baseLineIv = (ImageView) findViewById(R.id.base_line_iv);

        //标题栏右边文字控件
        barRightTx = (TextView) findViewById(R.id.bar_right_tx);
        //标题栏右边图片控件
        barRightIv = (ImageView) findViewById(R.id.bar_right_iv);
        //标题栏右边外层
        barRightRl = (RelativeLayout) findViewById(R.id.bar_right_rl);

        //基类内容显示区域
        rootContentLl = (LinearLayout) findViewById(R.id.root_content_ll);
        topLayout = (LinearLayout) findViewById(R.id.base_top_ll);

        //子类布局
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layoutResID, null);
        v.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        rootContentLl.addView(v);
    }

    /********************** 基类生命周期方法***********************/

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(mContext);
        super.onDestroy();
    }

    /*********************基类Title控件操作***********************/

    public RelativeLayout getRootBarRl() {
        return rootBarRl;
    }

    public LinearLayout getRootContentLl() {
        return rootContentLl;
    }

    /**
     * 返回按钮
     */
    protected LinearLayout getBarBackLl() {
        return barBackLl;
    }

    /**
     * 影藏头部
     */
    protected void hideTitleBar() {
        if (rootBarRl != null) {
            rootBarRl.setVisibility(View.GONE);
            baseLineIv.setVisibility(View.GONE);
        }
    }

    /**
     * 显示头部
     */
    protected void showTitleBar() {
        if (rootBarRl != null) {
            rootBarRl.setVisibility(View.VISIBLE);
            baseLineIv.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示标题文字，隐藏bar图片
     */
    public TextView setBarTitleTx(String str) {
        barTitleTx.setVisibility(View.VISIBLE);
        barTitleIv.setVisibility(View.GONE);
        barTitleTx.setText(str);
        return barTitleTx;
    }

    /**
     * 显示标题文字，隐藏bar图片
     */
    public TextView setBarTitleTx(int id) {
        barTitleTx.setVisibility(View.VISIBLE);
        barTitleIv.setVisibility(View.GONE);
        barTitleTx.setText(getResources().getString(id));
        return barTitleTx;
    }

    /**
     * 显示标题图片，隐藏标题文字
     */
    public ImageView setBarTitleIv(int id) {
        barTitleTx.setVisibility(View.GONE);
        barTitleIv.setVisibility(View.VISIBLE);
        barTitleIv.setImageResource(id);
        return barTitleIv;
    }

    /**
     * 显示标题右边文字，隐藏标题右边图片
     */
    protected ImageView setBarRightIv() {
        barRightIv.setVisibility(View.VISIBLE);
        barRightTx.setVisibility(View.GONE);
        return barRightIv;
    }

    /**
     * 显示标题右边文字，隐藏标题右边图片
     */
    protected ImageView setBarRightIv(int id) {
        barRightIv.setVisibility(View.VISIBLE);
        barRightTx.setVisibility(View.GONE);
        barRightIv.setImageResource(id);
        return barRightIv;
    }

    /**
     * 显示标题右边
     */
    protected RelativeLayout getBarRightRl() {
        barRightRl.setVisibility(View.VISIBLE);
        return barRightRl;
    }

    /**
     * 显示标题右边图片，隐藏标题右边文字
     */
    protected TextView setBarRightTx(String txt) {
        barRightTx.setVisibility(View.VISIBLE);
        barRightIv.setVisibility(View.GONE);
        barRightTx.setText(txt);
        return barRightTx;
    }

    /**
     * 显示标题右边图片，隐藏标题右边文字
     */
    protected TextView setBarRightTx(int id) {
        barRightTx.setVisibility(View.VISIBLE);
        barRightIv.setVisibility(View.GONE);
        barRightTx.setText(getResources().getString(id));
        return barRightTx;
    }

    public ImageView getBarHeightIv() {
        return barHeightIv;
    }

    /********************** 基类帮助方法***********************/


    //在注册了的Activity中,声明处理事件的方法
    @Subscribe(threadMode = ThreadMode.MAIN) //第2步:注册一个在后台线程执行的方法,用于接收事件
    public void onUserEvent(BaseEvent baseEvent) {//参数必须是ClassEvent类型, 否则不会调用此方法
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && (mAty instanceof MainActivity)) {// 返回键
            exitBy2Click();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "One more exit program", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            System.exit(0);
        }
    }

    protected boolean isTopActivity() {
        boolean isTop = false;
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        if (cn.getClassName().contains(mAty.getLocalClassName())) {
            isTop = true;
        }
        return isTop;
    }

    public int getStateBarHeight() {

        /**
         * 获取状态栏高度——方法1
         * */
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight1;
    }
}
